package guesswords.khova.com;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.util.Log;


/**
 * The simple to use XML Data reader. This class loads an XML file into memory
 * and allows you to manipulate it in tree form.
 * 
 * @author FanFan Huang
 *
 */
public class XMLData {
	/**
	 * Node value
	 */
	public String mValue;
	
	/**
	 * Node parent
	 */
	public XMLData mParent;
	
	/**
	 * Sub Nodes
	 */
	public List<XMLData> mChild;
	
	
	/**
	 * Constructor
	 * @param value Current Node's value
	 * @param parent Current Node's parent (null for root)
	 */
	public XMLData(String value, XMLData parent) {
		mParent = parent;
		mChild = new ArrayList<XMLData>();
		mValue = value;
	}
	
	/**
	 * Gets a string representation of the tree
	 */
	@Override
	public String toString() {
		return toString("");
	}
	
	public String toString(String prefix)
	{
		String output = "";
		if (mChild.size() == 0) {
			output = prefix + mValue + "\n";
		}
		else {
			output = prefix + mValue + "\n";
			for (XMLData child : mChild) {
				output += child.toString(prefix + "\t");
			}
		}
		
		return output;
	}
	
	/**
	 * Load an XMLData object from an XMLResourceParser
	 * 
	 * @param parser
	 * @return a full XMLData tree representation
	 */
	public static XMLData Load(XmlPullParser parser) {
		XMLData xmlModel = null;
		XMLData currentNode = null;
		
		/**
		 * How this works: 
		 * 
		 * 		The load routine builds an XMLData tree by traversing the
		 * 		entire xml file using the XMLPullParser in a serial fashion
		 * 
		 * 		Warning: There is no support for attributes as of this time.
		 */
		
		
		try
		{
			int eventType = parser.getEventType();
			
	        while (eventType != XmlPullParser.END_DOCUMENT){
	        	String name = null;
	                switch (eventType)	{
	                    case XmlPullParser.START_DOCUMENT:
	                        xmlModel = new XMLData("root", null);
	                        currentNode = xmlModel;
	                        break;
	                    case XmlPullParser.START_TAG:
	                        name = parser.getName();
	                        
	                        if (name != null) {
	                        	XMLData dataNode = new XMLData(name, currentNode);
	                        	currentNode.mChild.add(dataNode);
	                        	currentNode = dataNode;
	                        }
	                        break;
	                        
	                    case XmlPullParser.TEXT:
	                    	name = parser.getText();
	                    	
	                    	if (name != null) {
	                    		XMLData fieldNode = new XMLData(name,currentNode);
	                    		currentNode.mChild.add(fieldNode);
	                    	}
	                    	break;
	                    	
	                    case XmlPullParser.END_TAG:
	                        name = parser.getName();
	                        
	                        currentNode = currentNode.mParent;
	                        break;
	                }
	                
	                eventType = parser.next();
	        }
	    } catch (XmlPullParserException e) {
	        Log.e("ParseEx", e.getMessage());
	    } catch (IOException e) {
	    	Log.e("IOEx", e.getMessage());
	    }
	    

		return xmlModel;
	}
}
